﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using UCNKompetenceBors.Model;
using UCNKompetenceBors.Database;

namespace UCNKompetenceBors.Exchange
{
    public partial class saleCreate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ckeditorCreateSaleDescription.Width = 600;
                this.Form.DefaultButton = btnCreateSale.UniqueID;
                lblCreateSaleSuccess.Visible = false;
                PopulateCategoryDropDown();

                #region Globalization
                string culture = String.Empty;

                if (HttpContext.Current.Session["MyCulture"] != null)
                    culture = HttpContext.Current.Session["MyCulture"].ToString();

                if(!String.IsNullOrEmpty(culture))
                    GeneralTools.SetCulture(culture);

                LocalizeControls();
                #endregion
            }
        }

        /// <summary>
        /// Populates the category dropdown list.
        /// </summary>
        private void PopulateCategoryDropDown()
        {
            List<Category> categories = dbExchange.GetAllCategories();

            string culture = String.Empty;

            if (HttpContext.Current.Session["MyCulture"] != null)
                culture = HttpContext.Current.Session["MyCulture"].ToString();

            if (categories.Count > 0)
            {
                ddlCreateSaleCategory.DataSource = categories;
                if(!culture.Equals("en-GB"))
                    ddlCreateSaleCategory.DataTextField = "CategoryName";
                else
                    ddlCreateSaleCategory.DataTextField = "LocalizedCategoryName";

                ddlCreateSaleCategory.DataValueField = "CategoryID";
                ddlCreateSaleCategory.DataBind();

                ddlCreateSaleCategory.Items.Insert(0, new ListItem("Vælg Kategori"));
            }
        }

        /// <summary>
        /// Creates a sale.
        /// </summary>
        private void CreateSale()
        {
            string title = txtCreateSaleTitle.Text;
            string desc = ckeditorCreateSaleDescription.Text;
            int categoryId = Convert.ToInt32(ddlCreateSaleCategory.SelectedValue);

            dbExchange.CreateSale(title, desc, categoryId);
        }

        protected void btnCreateSale_Click(object sender, EventArgs e)
        {
            CreateSale();
            lblCreateSaleSuccess.Visible = true;
            panCreateSaleForm.Visible = false;
        }

        /// <summary>
        /// Localizes the controls of the site, based on the current UI Culture.
        /// </summary>
        private void LocalizeControls()
        {
            btnCreateSale.Text = GetLocalResourceObject("btnCreateSaleText").ToString();
            lblCreateSaleCategory.Text = GetLocalResourceObject("lblCreateSaleCategoryText").ToString();
            lblCreateSaleDescription.Text = GetLocalResourceObject("lblCreateSaleDescriptionText").ToString();
            lblCreateSaleHeader.Text = GetLocalResourceObject("lblCreateSaleHeaderText").ToString();
            lblCreateSaleSuccess.Text = GetLocalResourceObject("lblCreateSaleSuccessText").ToString();
            lblCreateSaleTitle.Text = GetLocalResourceObject("lblCreateSaleTitleText").ToString();
            
            string categoryValidation = GetLocalResourceObject("rfvalCreateSaleCategoryText").ToString();
            rfvalCreateSaleCategory.ToolTip = categoryValidation;
            rfvalCreateSaleCategory.ErrorMessage = categoryValidation;
            
            string descriptionValidation = GetLocalResourceObject("rfvalCreateSaleDescriptionText").ToString();
            rfvalCreateSaleDescription.ToolTip = descriptionValidation;
            rfvalCreateSaleDescription.ErrorMessage = descriptionValidation;
            
            string titleValidation = GetLocalResourceObject("rfvalCreateSaleTitleText").ToString();
            rfvalCreateSaleTitle.ToolTip = titleValidation;
            rfvalCreateSaleTitle.ErrorMessage = titleValidation;
            valsumCreateSale.HeaderText = GetLocalResourceObject("valsumCreateSaleText").ToString();

            string categoryDummy = GetLocalResourceObject("ddlCreateSaleCategoryDummyObjectText").ToString();
            ddlCreateSaleCategory.Items[0].Text = categoryDummy;
            rfvalCreateSaleCategory.InitialValue = categoryDummy;

            string culture = String.Empty;

            if (HttpContext.Current.Session["MyCulture"] != null)
                culture = HttpContext.Current.Session["MyCulture"].ToString();

            if (culture.Equals("en-GB"))
                ckeditorCreateSaleDescription.Language = "en";
            else
                ckeditorCreateSaleDescription.Language = "da";
        }
    }
}